import Vue from 'vue'
import Component from './func-notifiction'

const NotifictionConstructor = Vue.extend(Component)

const instances = []

let seed = 1
const notify = (options) => {
  const {
    autoClose,
    ...rest
  } = options
  const instance = new NotifictionConstructor({
    propsData: {...rest},
    data () {
      return {
        timeoutParam: autoClose === undefined ? 3000 : autoClose
      }
    },
    mounted () {
      if (this.timeoutParam) {
        this.timer = setTimeout(() => {
          this.visible = false
        }, this.timeoutParam);
      }
    },
    beforeDestroy () {
      if (this.timer) {
        clearTimeout(this.timer)
      }
    }
  })
  console.log(instance, 'instance')
  const id = `notifiction_${seed++}`
  instance.id = id
  instance.vm = instance.$mount()// 生生成$el标签
  document.body.appendChild(instance.vm.$el)

  let vertical = 0;
  instances.forEach(item => {
    vertical += item.$el.offsetHeight + 16
  })
  instance.verticalOffset = vertical
  vertical += 16
  instances.push(instance)
  return instance.vm
}

export default notify
